package com.enqualcomm.kids.view.fencing;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.network.socket.response.FencingListResult;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by yinzhiqun on 2016/3/5.
 * 地图下方，显示电子围栏名称，地址，禁用时段
 */
public class FencingPanelView extends LinearLayout implements IFencingPanelView{

private Context context;
    public FencingPanelView(Context context) {
        this(context, null);
    }

    public FencingPanelView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FencingPanelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(context);
    }

    private void init(Context context) {
        setOrientation(VERTICAL);
//        setBackgroundColor(Color.rgb(255, 255, 255));
        setShowDividers(SHOW_DIVIDER_MIDDLE);

        View topView = View.inflate(context, R.layout.fencing_panel_part1, null);
        fencing_name_tv = (TextView) topView.findViewById(R.id.fencing_name_tv);
        fencing_icon_iv = (ImageView) topView.findViewById(R.id.fencing_icon_iv);
        fencing_address_tv = (TextView) topView.findViewById(R.id.fencing_address_tv);

        bottomView = View.inflate(context, R.layout.fencing_panel_part2, null);
        bottomView.setVisibility(View.GONE);
        silence_tv1 = (TextView) bottomView.findViewById(R.id.silence_tv1);
        silence_tv1.setTag(context.getString(R.string.morning_begin_time));
        silence_tv2 = (TextView) bottomView.findViewById(R.id.silence_tv2);
        silence_tv2.setTag(context.getString(R.string.morning_end_time));
        silence_tv3 = (TextView) bottomView.findViewById(R.id.silence_tv3);
        silence_tv3.setTag(context.getString(R.string.noon_begin_time));
        silence_tv4 = (TextView) bottomView.findViewById(R.id.silence_tv4);
        silence_tv4.setTag(context.getString(R.string.morning_end_time));

        addView(topView);
        addView(bottomView);
    }


    TextView fencing_name_tv;
    ImageView fencing_icon_iv;
    TextView fencing_address_tv;
    View bottomView;
    TextView silence_tv1;
    TextView silence_tv2;
    TextView silence_tv3;
    TextView silence_tv4;
    boolean clickable;//学校和家，不允许修改成其他名字

    @Override
    public Observable<TextView> fencingNameEvents() {
        return RxView.clicks(fencing_name_tv).filter(new Func1<Void, Boolean>() {
            @Override
            public Boolean call(Void aVoid) {
                return clickable;
            }
        }).throttleFirst(500, TimeUnit.MILLISECONDS).map(new Func1<Void, TextView>() {
            @Override
            public TextView call(Void aVoid) {
                return fencing_name_tv;
            }
        });
    }

    @Override
    public String getFencingName() {
        return fencing_name_tv.getText().toString();
    }

    String[] silenceTimes;

    @Override
    public void bindData(FencingListResult.Data fencing) {
        fencing_name_tv.setText(fencing.fencingname);
        if (fencing.fencingcode==2) {
            fencing_icon_iv.setBackgroundResource(R.drawable.fencing_default_home);
            showSilence(false);
            clickable = false;
            findViewById(R.id.fencing_name_edit_tv).setVisibility(View.INVISIBLE);

        } else if (fencing.fencingcode==1) {
            fencing_icon_iv.setBackgroundResource(R.drawable.fencing_default_school);
            showSilence(false);
            clickable = false;
            findViewById(R.id.fencing_name_edit_tv).setVisibility(View.INVISIBLE);

            //设置上课禁用
//            silenceTimes = fencing.silenceTimes;
//            silence_tv1.setText(fencing.silenceTimes[0]);
//            silence_tv2.setText(fencing.silenceTimes[1]);
//            silence_tv3.setText(fencing.silenceTimes[2]);
//            silence_tv4.setText(fencing.silenceTimes[3]);
        } else {
            fencing_icon_iv.setBackgroundResource(R.drawable.safezoom_icon);
            showSilence(false);
            clickable = true;
        }
    }

    @Override
    public Observable<TextView> silenceTimeEvents() {
        return RxView.clicks(silence_tv1).map(new Func1<Void, TextView>() {
            @Override
            public TextView call(Void aVoid) {
                return silence_tv1;
            }
        }).mergeWith(RxView.clicks(silence_tv2).map(new Func1<Void, TextView>() {
            @Override
            public TextView call(Void aVoid) {
                return silence_tv2;
            }
        })).mergeWith(RxView.clicks(silence_tv3).map(new Func1<Void, TextView>() {
            @Override
            public TextView call(Void aVoid) {
                return silence_tv3;
            }
        })).mergeWith(RxView.clicks(silence_tv4).map(new Func1<Void, TextView>() {
            @Override
            public TextView call(Void aVoid) {
                return silence_tv4;
            }
        })).throttleFirst(500, TimeUnit.MILLISECONDS);
    }

    @Override
    public String[] getSilenceTime() {
        if (silenceTimes!=null){
            silenceTimes[0] = silence_tv1.getText().toString();
            silenceTimes[1] = silence_tv2.getText().toString();
            silenceTimes[2] = silence_tv3.getText().toString();
            silenceTimes[3] = silence_tv4.getText().toString();
            return silenceTimes;
        }else{
            return null;
        }
    }

    @Override
    public void setAddress(String address) {
        fencing_address_tv.setText(address);
    }

    /**
     * 设置是否展示上课禁用
     * @param b
     */
    public void showSilence(boolean b) {
        bottomView.setVisibility(b?View.VISIBLE:View.GONE);
    }
}
